﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Entity;
using NLBS.Framework;
using System.Web;
using NLBS.Data.Business;
using NLBS.Data;
using NLBS.Views;
using NLBS.Text;
using NLBS.Web;
using NLBS.Security;

namespace NLBS.Validate
{
    public class CommentAddValidate : ValidateTemplete<Comment>
    {



        private ICommentFormView commentAction;
        private ICommentAction<Comment> comm;

        public CommentAddValidate() {
        
        }
        public CommentAddValidate(ICommentFormView act)
        {
            commentAction = act;
        }
        public CommentAddValidate(ICommentFormView act, ICommentAction<Comment> comm)
        {
            commentAction = act;
            this.comm = comm;
        }
        /// <summary>
        /// 检测评论权限
        /// </summary>
        public override void checkPower()
        {
            if (power.Post < 1 || BlogInfo.Instance().EnableComment == 0 || String.IsNullOrEmpty(GlobalData.CheckCode))
            {
                throw new Exception("没有权限进行此操作");
            }
        }
        /// <summary>
        /// 检测评论间隔时间
        /// </summary>
        public void checkPostDuration()
        {
            ValidateHelper.checkPostDuration();
        }
        /// <summary>
        /// 检测用户是否登录
        /// </summary>
        public void checkLogin()
        {
            //判断是否登录
            if (!HttpContext.Current.Request.IsAuthenticated)
            {
                //判断是否要注册
                if (commentAction.register)
                {
                }
                else if(!string.IsNullOrEmpty(commentAction.password))
                {
                    //若评论用户填写了密码,则验证用户信息
                    validateUser();
                } else
                {
                    //检测用户名是否重复
                    checkExistName();
                }
            }
        }

        /// <summary>
        /// 检测用户帐号密码
        /// </summary>
        public void validateUser()
        {
            bool isvalid = DbProvider.Instance().ValidateUser(commentAction.username,StringSecurity.StringToSHA1Hash(commentAction.password));
            if (!isvalid)
                throw new Exception("用户名和密码不匹配. 登录失败.");
        }
        /// <summary>
        /// 检测用户是否存在
        /// </summary>
        public void checkExistName()
        {
            User user = UserData.GetUserByName(commentAction.username);
          if (user != null)
          {
              throw new Exception("用户已存在");
          }
          else
          {
              BlogInfo.Instance().CurrentUser.UserID = 0;
              BlogInfo.Instance().CurrentUser.UserName = commentAction.username;
          }
        }
        /// <summary>
        /// 检测表单字段
        /// </summary>
        public override void checkArgument()
        {
            // Check Security Code
            string a = GlobalData.CheckCode;
            string b = RequestWrapper.GetFormString("scode");
            
            if (BlogInfo.Instance().EnableSecurityCode == 1 && GlobalData.CheckCode != RequestWrapper.GetFormString("scode"))
            {
                Error += "<li>" + lang["scode_invalid"] + "</li>";
            }
            string content =this.commentAction.content.Trim();
            if (content.Length == 0)
            {
                Error += "<li>" + lang["content_blank"] + "</li>";
            }
            else
            {
                //检测评论内容长度
                int contentLength = content.Length;
                if (contentLength > BlogInfo.Instance().MaxCommentLength || contentLength < 2)
                {
                    Error += "<li>" + lang["length_invalid"] + "</li>";
                }
                //检测Url个数
                if (HtmlHelper.CheckHyperlinkCount(content) > BlogInfo.Instance().MaxCommentURLCount)
                {
                    Error += "<li>" + lang["comment_url_count_block"] + "</li>";
                }

                // Exit with error so we don't need to query the db any more
                if (!String.IsNullOrEmpty(Error))
                {
                    throw new Exception(Error);
                }
                //内容过滤
                //input["message"] = func.wordFilter(input["message"]);
                //if (!input["message"])
                //{
                //    strError += "<li>" + lang["wordfilter_block"] + "</li>";
                //}
                
               
            }
        }
        /// <summary>
        /// 检测日志是否存在和浏览权限
        /// </summary>
        public override void checkEntity()
        {
            //检测日志是否存在
            Article art = ArticleData.GetArticleByID(comm.Entity.LogID);

            if (art == null)
            {
                Error = lang["article_not_found"];
                throw new Exception(Error);
            }
            
            //检测浏览权限
            if (!ViewHelp.checkViewPermission(art))
            {
                Error = "<li>" + lang["no_rights"] + "</li>";
                throw new Exception(Error);
            }
        }

        public override void checkPowerAgain()
        {
            throw new NotImplementedException();
        }

        public void Validate(CallValid valid,CallError error)
        {
            try
            {
               
                checkPower();
                checkPostDuration();
                checkLogin();
                checkArgument();
                checkEntity();
            }
            catch (System.Exception e)
            {
                error(e.Message);
            }
            valid();
        }
    }
}
